﻿using Li.DingTalk.Model.Contacts;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using static Li.DingTalk.Model.Contacts.attendanceHD;
using static Li.DingTalk.Model.Contacts.chatHD;
using static Discuz.Common.Utils;
using DingTalk.Api;
using DingTalk.Api.Request;
using DingTalk.Api.Response;

namespace DingTalkAgent.Web.Controllers
{
    public class HomeController : BaseController
    {
        protected string _encryptKey = "wbg!@#dmkj%^&20150501#$k(";

        // GET: Admin/home
        public ActionResult Index()
        {
            var CorpID = ConfigHelper.FetchCorpID();
            //检查是否注册
            if (!Li.DingTalk.AccessTokenContainer.CheckRegistered(CorpID))
            {
                //查询CorpSecret
                var CorpSecret = ConfigHelper.FetchCorpSecret();
                Li.DingTalk.AccessTokenContainer.Register(CorpID, CorpSecret);
            }
            //获取 AccessToken
            string AccessToken = Li.DingTalk.AccessTokenContainer.TryGetToken(CorpID);
            ViewBag.access_token = AccessToken;

            return View();
        }

        /// <summary>
        /// 加密工具
        /// </summary>
        /// <returns></returns>
        public ActionResult Encode(string str)
        {
            return Content(Discuz.Common.AES.Encode(str, _encryptKey));
        }


        #region 通讯录

        #region 人员管理
        /// <summary>
        /// 获取人员信息
        /// </summary>
        /// <param name="id">员工在企业内的UserID</param>
        /// <returns></returns>
        public ActionResult getuserInfo(string userid)
        {
            var access_token = Li.DingTalk.AccessTokenContainer.TryGetToken(_view_account.corpId);
            var result = Li.DingTalk.Model.Contacts.userHD.GetUserInfo(access_token, userid);
            if (result.IsOK())
            {
                return PartialView(result);
            }
            else
            {
                return Content("获取成员信息失败");
            }
        }

        /// <summary>
        /// 创建成员
        /// </summary>
        /// <param name="model">创建人员信息的结构体</param>
        /// <returns></returns>
        public ActionResult CreateUser(creatememberstructural model)
        {
            var access_token = Li.DingTalk.AccessTokenContainer.TryGetToken(_view_account.corpId);
            var result = Li.DingTalk.Model.Contacts.userHD.Createmember(access_token, model);
            if (result.IsOK())
            {
                return PartialView(result);
            }
            else
            {
                return Content("<script>window.parent.alert('创建人员失败!')</script>");
            }
        }

        /// <summary>
        /// 更新成员
        /// </summary>
        /// <param name="model">更新人员信息的结构体</param>
        /// <returns></returns>
        public ActionResult UpdateUser(updatememberstructural model)
        {
            var access_token = Li.DingTalk.AccessTokenContainer.TryGetToken(_view_account.corpId);
            var result = Li.DingTalk.Model.Contacts.userHD.Updatemember(access_token, model);
            if (result.IsOK())
            {
                return Content("<script>window.parent.alert('更新成功!')</script>");
            }
            else
            {
                return Content("<script>window.parent.alert('更新人员失败!')</script>");
            }
        }

        /// <summary>
        /// 删除成员
        /// </summary>
        /// <param name="id">员工在企业内的UserID</param>
        /// <returns></returns>
        public ActionResult DeleteUser(string userid)
        {
            var access_token = Li.DingTalk.AccessTokenContainer.TryGetToken(_view_account.corpId);
            var result = Li.DingTalk.Model.Contacts.userHD.Deletemember(access_token, userid);
            if (result.IsOK())
            {
                return Content("<script>window.parent.alert('删除成员成功！')</script>");
            }
            else
            {
                return Content("<script>window.parent.alert('删除成员失败！')</script>");
            }
        }

        /// <summary>
        /// 批量删除成员
        /// </summary>
        /// <param name="model">批量删除人员信息的结构体</param>
        /// <returns></returns>
        public ActionResult DeleteAllUser(batchdeletionstructural model)
        {
            var access_token = Li.DingTalk.AccessTokenContainer.TryGetToken(_view_account.corpId);
            var result = Li.DingTalk.Model.Contacts.userHD.batchdeletion(access_token, model);
            if (result.IsOK())
            {
                return Content("<script>window.parent.alert('批量删除成员成功!')</script>");
            }
            else
            {
                return Content("<script>window.parent.alert('批量删除成员失败!')</script>");
            }
        }

        /// <summary>
        /// 获取部门成员
        /// </summary>
        /// <param name="department_id">部门id</param>
        /// <returns></returns>
        public ActionResult GetDepartmentMember(long department_id)
        {
            var access_token = Li.DingTalk.AccessTokenContainer.TryGetToken(_view_account.corpId);
            var result = Li.DingTalk.Model.Contacts.userHD.Getdepartmentmember(access_token, department_id);
            if (result.IsOK())
            {
                return PartialView(result);
            }
            else
            {
                return Content("<script>window.parent.alert('获取部门成员失败!')</script>");
            }
        }

        /// <summary>
        /// 获取部门成员详情
        /// </summary>
        /// <param name="department_id">部门id</param>
        /// <returns></returns>
        public ActionResult GetDepartmentMemberList(long department_id)
        {
            var access_token = Li.DingTalk.AccessTokenContainer.TryGetToken(_view_account.corpId);
            var result = Li.DingTalk.Model.Contacts.userHD.Getdepartmentmemberlist(access_token, department_id);
            if (result.IsOK())
            {
                return PartialView(result);
            }
            else
            {
                return Content("<script>window.parent.alert('获取部门成员详情失败!')</script>");
            }
        }

        /// <summary>
        /// 获取管理员列表
        /// </summary>
        /// <returns></returns>
        public ActionResult GetAdministratorList()
        {
            var access_token = Li.DingTalk.AccessTokenContainer.TryGetToken(_view_account.corpId);
            var result = Li.DingTalk.Model.Contacts.userHD.Getadministratorlist(access_token);
            if (result.IsOK())
            {
                return PartialView(result);
            }
            else
            {
                return Content("<script>window.parent.alert('获取管理员列表失败!')</script>");
            }
        }

        /// <summary>
        /// 根据unionid获取成员的userid
        /// </summary>
        /// <param name="unionid">用户在当前钉钉开放平台账号范围内的唯一标识，同一个钉钉开放平台账号可以包含多个开放应用，同时也包含ISV的套件应用及企业应用</param>
        /// <returns></returns>
        public ActionResult GetUseridByUnionid(string unionid)
        {
            var access_token = Li.DingTalk.AccessTokenContainer.TryGetToken(_view_account.corpId);
            var result = Li.DingTalk.Model.Contacts.userHD.Getuseridbyunionid(access_token, unionid);
            if (result.IsOK())
            {
                return PartialView(result);
            }
            else
            {
                return Content("<script>window.parent.alert('获取成员的userid失败!')</script>");
            }
        }

        /// <summary>
        /// 获取企业员工人数
        /// </summary>
        /// <param name="onlyActive"></param>
        /// <returns></returns>
        public ActionResult GetUserNumber(int onlyActive)
        {
            var access_token = Li.DingTalk.AccessTokenContainer.TryGetToken(_view_account.corpId);
            var result = Li.DingTalk.Model.Contacts.userHD.Getusernumber(access_token, onlyActive);
            if (result.IsOK())
            {
                return PartialView(result);
            }
            else
            {
                return Content("<script>window.parent.alert('获取企业员工人数失败!')</script>");
            }
        }

        #endregion

        #region 部门管理

        /// <summary>
        /// 获取部门列表
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult GetDepartmentList(string id)
        {
            var access_token = Li.DingTalk.AccessTokenContainer.TryGetToken(_view_account.corpId);
            var result = Li.DingTalk.Model.Contacts.departmentHD.list(access_token, id);
            if (result.IsOK())
            {
                return PartialView(result);
            }
            else
            {
                return Content("获取部门列表失败");
            }
        }

        /// <summary>
        /// 获取部门详情
        /// </summary>
        /// <param name="id">部门id</param>
        /// <returns></returns>
        public ActionResult GetDepartmentDetails(string id)
        {
            var access_token = Li.DingTalk.AccessTokenContainer.TryGetToken(_view_account.corpId);
            var result = Li.DingTalk.Model.Contacts.departmentHD.Getdepartmentlist(access_token, id);
            if (result.IsOK())
            {
                return PartialView(result);
                //return Content("<script>window.parent.alert('获取部门详情成功!')</script>");
            }
            else
            {
                return Content("获取部门详情失败");
            }
        }

        /// <summary>
        /// 创建部门
        /// </summary>
        /// <param name="model">创建部门的结构体</param>
        /// <returns></returns>
        public ActionResult CreateDepartments(createdepartmentstructural model)
        {
            var access_token = Li.DingTalk.AccessTokenContainer.TryGetToken(_view_account.corpId);
            var result = Li.DingTalk.Model.Contacts.departmentHD.Createdepartment(access_token, model);
            if (result.IsOK())
            {
                return PartialView("getcreatedepartlist", result);
                //return Content("<script>window.parent.alert('创建部门成功!')</script>");
            }
            else
            {
                return Content("<script>window.parent.alert('创建部门失败!')</script>");
            }
        }

        /// <summary>
        /// 更新部门
        /// </summary>
        /// <param name="model">更新部门的结构体</param>
        /// <returns></returns>
        public ActionResult UpdateDepartments(updatedepartmentstructural model)
        {
            var access_token = Li.DingTalk.AccessTokenContainer.TryGetToken(_view_account.corpId);
            var result = Li.DingTalk.Model.Contacts.departmentHD.Updatepartment(access_token, model);
            if (result.IsOK())
            {
                return Content("<script>window.parent.alert('更新部门成功!')</script>");
            }
            else
            {
                return Content("<script>window.parent.alert('更新部门失败!')</script>");
            }
        }

        /// <summary>
        /// 删除部门
        /// </summary>
        /// <param name="id">部门id</param>
        /// <returns></returns>
        public ActionResult DeleteDepartments(long id)
        {
            var access_token = Li.DingTalk.AccessTokenContainer.TryGetToken(_view_account.corpId);
            var result = Li.DingTalk.Model.Contacts.departmentHD.Deletedepartment(access_token, id);
            if (result.IsOK())
            {
                return Content("<script>window.parent.alert('删除部门成功!')</script>");
            }
            else
            {
                return Content("<script>window.parent.alert('删除部门失败!')</script>");
            }
        }

        /// <summary>
        /// 查询部门的所有上级父部门路径
        /// </summary>
        /// <param name="id">部门id</param>
        /// <returns></returns>
        public ActionResult QueryAllParentDepartment(long id)
        {
            var access_token = Li.DingTalk.AccessTokenContainer.TryGetToken(_view_account.corpId);
            var result = Li.DingTalk.Model.Contacts.departmentHD.Queryallparentdepartment(access_token, id);
            if (result.IsOK())
            {
                return PartialView(result);
            }
            else
            {
                return Content("<script>window.parent.alert('查询部门的所有上级父部门路径失败!')</script>");
            }
        }

        /// <summary>
        /// 查询指定用户的所有上级父部门路径
        /// </summary>
        /// <param name="userid">希望查询的用户的id</param>
        /// <returns></returns>
        public ActionResult QueryAppointParentDepartment(string userid)
        {
            var access_token = Li.DingTalk.AccessTokenContainer.TryGetToken(_view_account.corpId);
            var result = Li.DingTalk.Model.Contacts.departmentHD.Queryappointparentdepartment(access_token, userid);
            if (result.IsOK())
            {
                return PartialView(result);
            }
            else
            {
                return Content("<script>window.parent.alert('查询指定用户的所有上级父部门路径失败!')</script>");
            }
        }

        #endregion

        #region 权限管理
        /// <summary>
        /// 获取通讯录权限
        /// </summary>
        /// <param></param>
        /// <returns></returns>
        public ActionResult GetAddressJurisdiction()
        {
            var access_token = Li.DingTalk.AccessTokenContainer.TryGetToken(_view_account.corpId);
            var result = Li.DingTalk.Model.Contacts.authHD.Getaddressjurisdiction(access_token);
            if (result.IsOK())
            {
                return Content("<script>window.parent.alert('获取通讯录权限成功!')</script>");
            }
            else
            {
                return Content("<script>window.parent.alert('获取通讯录权限失败!')</script>");
            }
        }
        #endregion

        #region 消息会话管理
        /// <summary>
        /// 创建会话
        /// </summary>
        /// <param name="model">创建会话的结构体</param>
        /// <param></param>
        /// <returns></returns>
        public ActionResult CreateConversation(sessioncreationstructural model)
        {
            var access_token = Li.DingTalk.AccessTokenContainer.TryGetToken(_view_account.corpId);
            var result = Li.DingTalk.Model.Contacts.chatHD.Createconversation(access_token, model);
            if (result.IsOK())
            {
                return PartialView(result);
            }
            else
            {
                return Content("<script>window.parent.alert('创建会话失败!')</script>");
            }
        }

        /// <summary>
        /// 修改会话
        /// </summary>
        /// <param name="model">修改会话的结构体</param>
        /// <param></param>
        /// <returns></returns>
        public ActionResult ModifyConversation(modifysessionstructural model)
        {
            var access_token = Li.DingTalk.AccessTokenContainer.TryGetToken(_view_account.corpId);
            var result = Li.DingTalk.Model.Contacts.chatHD.Modifyconversation(access_token, model);
            if (result.IsOK())
            {
                return Content("<script>window.parent.alert('修改会话成功!')</script>");
            }
            else
            {
                return Content("<script>window.parent.alert('修改会话失败!')</script>");
            }
        }

        /// <summary>
        /// 获取会话
        /// </summary>
        /// <param name="chatid">群会话的id</param>
        /// <param></param>
        /// <returns></returns>
        public ActionResult ObtainConversation(string chatid)
        {
            var access_token = Li.DingTalk.AccessTokenContainer.TryGetToken(_view_account.corpId);
            var result = Li.DingTalk.Model.Contacts.chatHD.Obtainconversation(access_token, chatid);
            if (result.IsOK())
            {
                return PartialView(result);
            }
            else
            {
                return Content("<script>window.parent.alert('获取会话失败!')</script>");
            }
        }

        /// <summary>
        /// 卡片消息无Action样式
        /// </summary>
        /// <param name="model">卡片消息无Action样式的结构体</param>
        /// <param></param>
        /// <returns></returns>
        public ActionResult NoAction(noactionstructural model)
        {
            var access_token = Li.DingTalk.AccessTokenContainer.TryGetToken(_view_account.corpId);
            var result = Li.DingTalk.Model.Contacts.chatHD.Noaction(access_token, model);
            if (result.IsOK())
            {
                return Content("<script>window.parent.alert('卡片消息无Action样式发送成功!')</script>");
            }
            else
            {
                return Content("<script>window.parent.alert('卡片消息无Action样式发送失败!')</script>");
            }
        }

        /// <summary>
        /// 卡片消息单一Action样式
        /// </summary>
        /// <param name="model">卡片消息单一Action样式的结构体</param>
        /// <param></param>
        /// <returns></returns>
        public ActionResult OneAction(oneactionstructural model)
        {
            var access_token = Li.DingTalk.AccessTokenContainer.TryGetToken(_view_account.corpId);
            var result = Li.DingTalk.Model.Contacts.chatHD.Oneaction(access_token, model);
            if (result.IsOK())
            {
                return Content("<script>window.parent.alert('卡片消息单一Action样式发送成功!')</script>");
            }
            else
            {
                return Content("<script>window.parent.alert('卡片消息单一Action样式发送失败!')</script>");
            }
        }

        /// <summary>
        /// 卡片消息多Action样式
        /// </summary>
        /// <param name="model">卡片消息多Action样式的结构体</param>
        /// <param></param>
        /// <returns></returns>
        public ActionResult ManyAction(manyactionstructural model)
        {
            var access_token = Li.DingTalk.AccessTokenContainer.TryGetToken(_view_account.corpId);
            var result = Li.DingTalk.Model.Contacts.chatHD.Manyaction(access_token, model);
            if (result.IsOK())
            {
                return Content("<script>window.parent.alert('卡片消息多Action样式发送成功!')</script>");
            }
            else
            {
                return Content("<script>window.parent.alert('卡片消息多Action样式发送失败!')</script>");
            }
        }

        /// <summary>
        /// 发送text消息
        /// </summary>
        /// <param name="model">发送text消息的结构体</param>
        /// <param></param>
        /// <returns></returns>
        public ActionResult TextMessage(textmessagestructural model)
        {
            var access_token = Li.DingTalk.AccessTokenContainer.TryGetToken(_view_account.corpId);
            var result = Li.DingTalk.Model.Contacts.chatHD.Textmessage(access_token, model);
            if (result.IsOK())
            {
                return Content("<script>window.parent.alert('text消息发送成功!')</script>");
            }
            else
            {
                return Content("<script>window.parent.alert('text消息发送失败!')</script>");
            }
        }

        /// <summary>
        /// 发送image消息
        /// </summary>
        /// <param name="model">发送image消息的结构体</param>
        /// <param></param>
        /// <returns></returns>
        public ActionResult ImgMessage(imgmessagestructural model)
        {
            var access_token = Li.DingTalk.AccessTokenContainer.TryGetToken(_view_account.corpId);
            var result = Li.DingTalk.Model.Contacts.chatHD.Imgmessage(access_token, model);
            if (result.IsOK())
            {
                return Content("<script>window.parent.alert('image消息发送成功!')</script>");
            }
            else
            {
                return Content("<script>window.parent.alert('image消息发送失败!')</script>");
            }
        }

        /// <summary>
        /// 发送voice消息
        /// </summary>
        /// <param name="model">发送voice消息的结构体</param>
        /// <param></param>
        /// <returns></returns>
        public ActionResult VoiceMessage(voicemessagestructural model)
        {
            var access_token = Li.DingTalk.AccessTokenContainer.TryGetToken(_view_account.corpId);
            var result = Li.DingTalk.Model.Contacts.chatHD.Voicemessage(access_token, model);
            if (result.IsOK())
            {
                return Content("<script>window.parent.alert('voice消息发送成功!')</script>");
            }
            else
            {
                return Content("<script>window.parent.alert('voice消息发送失败!')</script>");
            }
        }

        /// <summary>
        /// 发送file消息
        /// </summary>
        /// <param name="model">发送file消息的结构体</param>
        /// <param></param>
        /// <returns></returns>
        public ActionResult FileMessage(filemessagestructural model)
        {
            var access_token = Li.DingTalk.AccessTokenContainer.TryGetToken(_view_account.corpId);
            var result = Li.DingTalk.Model.Contacts.chatHD.Filemessage(access_token, model);
            if (result.IsOK())
            {
                return Content("<script>window.parent.alert('file消息发送成功!')</script>");
            }
            else
            {
                return Content("<script>window.parent.alert('file消息发送失败!')</script>");
            }
        }

        /// <summary>
        /// 发送link消息
        /// </summary>
        /// <param name="model">发送link消息的结构体</param>
        /// <param></param>
        /// <returns></returns>
        public ActionResult LinkMessage(linkmessagestructural model)
        {
            var access_token = Li.DingTalk.AccessTokenContainer.TryGetToken(_view_account.corpId);
            var result = Li.DingTalk.Model.Contacts.chatHD.Linkmessage(access_token, model);
            if (result.IsOK())
            {
                return Content("<script>window.parent.alert('link消息发送成功!')</script>");
            }
            else
            {
                return Content("<script>window.parent.alert('link消息发送失败!')</script>");
            }
        }

        /// <summary>
        /// 发送OA消息
        /// </summary>
        /// <param name="model">发送OA消息的结构体</param>
        /// <param></param>
        /// <returns></returns>
        public ActionResult OaMessage(oamessagestructural model)
        {
            var access_token = Li.DingTalk.AccessTokenContainer.TryGetToken(_view_account.corpId);
            var result = Li.DingTalk.Model.Contacts.chatHD.OAmessage(access_token, model);
            if (result.IsOK())
            {
                return Content("<script>window.parent.alert('oa消息发送成功!')</script>");
            }
            else
            {
                return Content("<script>window.parent.alert('oa消息发送失败!')</script>");
            }
        }

        /// <summary>
        /// 发送markdown消息
        /// </summary>
        /// <param name="model">发送markdown消息的结构体</param>
        /// <param></param>
        /// <returns></returns>
        public ActionResult MarkDownMessage(markdownmessagestructural model)
        {
            var access_token = Li.DingTalk.AccessTokenContainer.TryGetToken(_view_account.corpId);
            var result = Li.DingTalk.Model.Contacts.chatHD.Markdownmessage(access_token, model);
            if (result.IsOK())
            {
                return Content("<script>window.parent.alert('markdown消息发送成功!')</script>");
            }
            else
            {
                return Content("<script>window.parent.alert('markdown消息发送失败!')</script>");
            }
        }

        /// <summary>
        /// 发送普通消息
        /// </summary>
        /// <param name="model">发送普通消息的结构体</param>
        /// <param></param>
        /// <returns></returns>
        public ActionResult SendConversationMessage(sendconversationstructural model)
        {
            var access_token = Li.DingTalk.AccessTokenContainer.TryGetToken(_view_account.corpId);
            var result = Li.DingTalk.Model.Contacts.chatHD.Sendconversationmessage(access_token, model);
            if (result.IsOK())
            {
                return Content("<script>window.parent.alert('发送普通消息发送成功!')</script>");
            }
            else
            {
                return Content("<script>window.parent.alert('发送普通消息发送失败!')</script>");
            }
        }

        #endregion

        #endregion


        #region 智能办公

        #region 考勤管理

        /// <summary>
        /// 考勤打卡记录开放
        /// </summary>
        /// <param name="model">考勤打卡记录开放的结构体</param>
        /// <param></param>
        /// <returns></returns>
        public ActionResult AttendanceCardRecord(attendancecardrecordstructural model)
        {
            var access_token = Li.DingTalk.AccessTokenContainer.TryGetToken(_view_account.corpId);
            var result = Li.DingTalk.Model.Contacts.attendanceHD.Attendancecardrecord(access_token, model);
            if (result.IsOK())
            {
                return PartialView(result);
            }
            else
            {
                return Content("<script>window.parent.alert('获取考勤打卡记录失败!')</script>");
            }
        }

        /// <summary>
        /// 签到管理
        /// </summary>
        /// <param name="department_id">部门id</param>
        /// <param name="start_time">开始时间，精确到毫秒</param>
        /// <param name="end_time">结束时间，精确到毫秒（默认为当前时间）</param>
        /// <param name="offset">支持分页查询，与size 参数同时设置时才生效，此参数代表偏移量，从0 开始</param>
        /// <param name="size">支持分页查询，与offset 参数同时设置时才生效，此参数代表分页大小，最大100</param>
        /// <param name="order">排序，asc 为正序，desc 为倒序</param>
        /// <param></param>
        /// <returns></returns>
        public ActionResult SignList(string department_id, long start_time, long end_time, long offset, int size, string order)
        {
            DateTime t = DateTime.Now;    //当前时间
            DateTime t1 = t.AddHours(-1); //前一小时
            start_time = (long)ConvertToUnixTimestamp(t1);
            end_time = (long)ConvertToUnixTimestamp(t);

            var access_token = Li.DingTalk.AccessTokenContainer.TryGetToken(_view_account.corpId);
            var result = Li.DingTalk.Model.Contacts.attendanceHD.Signlist(access_token, department_id, start_time, end_time, offset, size, order);
            if (result.IsOK())
            {
                return PartialView(result);
            }
            else
            {
                return Content("<script>window.parent.alert('获取签到记录失败!')</script>");
            }
        }

        #endregion

        #endregion

        #region 文件管理

        #region 多媒体文件管理

        /// <summary>
        /// 上传媒体文件
        /// </summary>
        /// <param name="fileWithPath">上传文件的完整路径</param>
        /// <param name="type">媒体文件类型，分别有图片（image）、语音（voice）、普通文件(file)</param>
        /// <param></param>
        /// <returns></returns>
        public ActionResult UploadFiles(string fileWithPath, string type)
        {
            var access_token = Li.DingTalk.AccessTokenContainer.TryGetToken(_view_account.corpId);
            var result = Li.DingTalk.Model.Contacts.media.Uploadfiles(access_token, fileWithPath, type);
            if (result.IsOK())
            {
                return PartialView(result);
            }
            else
            {
                return Content("<script>window.parent.alert('上传文件失败!')</script>");
            }
        }

        /// <summary>
        /// 发送文件给指定用户
        /// </summary>
        /// <param name="agent_id">媒体文件类型，分别有图片（image）、语音（voice）、普通文件(file)</param>
        /// <param name="userid">媒体文件类型，分别有图片（image）、语音（voice）、普通文件(file)</param>
        /// <param name="media_id">媒体文件类型，分别有图片（image）、语音（voice）、普通文件(file)</param>
        /// <param name="file_name">媒体文件类型，分别有图片（image）、语音（voice）、普通文件(file)</param>
        /// <param></param>
        /// <returns></returns>
        public ActionResult SendFiles(string agent_id, string userid, string media_id, string file_name)
        {
            var access_token = Li.DingTalk.AccessTokenContainer.TryGetToken(_view_account.corpId);
            var result = Li.DingTalk.Model.Contacts.media.Sendfiles(access_token, agent_id, userid, media_id, file_name);
            if (result.IsOK())
            {
                return Content("<script>window.parent.alert('发送文件成功!')</script>");
            }
            else
            {
                return Content("<script>window.parent.alert('发送文件失败!')</script>");
            }
        }

        /// <summary>
        /// 新增文件到用户钉盘
        /// </summary>
        /// <param name="agent_id">媒体文件类型，分别有图片（image）、语音（voice）、普通文件(file)</param>
        /// <param name="code">媒体文件类型，分别有图片（image）、语音（voice）、普通文件(file)</param>
        /// <param name="media_id">媒体文件类型，分别有图片（image）、语音（voice）、普通文件(file)</param>
        /// <param name="space_id">媒体文件类型，分别有图片（image）、语音（voice）、普通文件(file)</param>
        /// <param name="folder_id">媒体文件类型，分别有图片（image）、语音（voice）、普通文件(file)</param>
        /// <param name="name">媒体文件类型，分别有图片（image）、语音（voice）、普通文件(file)</param>
        /// <param name="overwrite">媒体文件类型，分别有图片（image）、语音（voice）、普通文件(file)</param>
        /// <param></param>
        /// <returns></returns>
        public ActionResult AdduserFiles(string agent_id, string code, string media_id, string space_id, string folder_id, string name, string overwrite)
        {
            var access_token = Li.DingTalk.AccessTokenContainer.TryGetToken(_view_account.corpId);
            var result = Li.DingTalk.Model.Contacts.media.Adduserfiles(access_token, agent_id, code, media_id, space_id, folder_id, name, overwrite);
            if (result.IsOK())
            {
                return Content("<script>window.parent.alert('新增文件到用户钉盘成功!')</script>");
            }
            else
            {
                return Content("<script>window.parent.alert('新增文件到用户钉盘失败!')</script>");
            }
        }

        /// <summary>
        /// 获取企业下的自定义空间
        /// </summary>
        /// <param name="domain">企业调用时传入，需要为10个字节以内的字符串，仅可包含字母和数字，大小写不敏感</param>
        /// <param name="agent_id">	ISV调用时传入，微应用agentId</param>
        /// <param></param>
        /// <returns></returns>
        public ActionResult GetenterPriseSpace(string domain, string agent_id)
        {
            var access_token = Li.DingTalk.AccessTokenContainer.TryGetToken(_view_account.corpId);
            var result = Li.DingTalk.Model.Contacts.media.Getenterprisespace(access_token, domain, agent_id);
            if (result.IsOK())
            {
                return Content("<script>window.parent.alert('获取企业下的自定义空间成功!')</script>");
            }
            else
            {
                return Content("<script>window.parent.alert('获取企业下的自定义空间失败!')</script>");
            }
        }

        /// <summary>
        /// 授权用户访问企业下的自定义空间
        /// </summary>
        /// <param name="agent_id">ISV调用时传入，授权访问指定微应用的自定义空间</param>
        /// <param name="domain">企业调用时传入，授权访问该domain的自定义空间</param>
        /// <param name="type">	权限类型，目前支持上传和下载，上传请传add，下载请传download</param>
        /// <param name="userid">企业用户userid</param>
        /// <param name="path">	授权访问的路径，如授权访问所有文件传“/”，授权访问/doc文件夹传“/doc/” 需要utf-8 urlEncode</param>
        /// <param name="fileids">授权访问的文件id列表，id之间用英文逗号隔开，如“fileId1,fileId2”</param>
        /// <param name="duration">	权限有效时间，有效范围为0~3600秒，超出此范围或不传默认为30秒</param>
        /// <param></param>
        /// <returns></returns>
        public ActionResult GetUserenterPriseSpace(string agent_id, string domain, string type, string userid, string path, string fileids, int duration)
        {
            var access_token = Li.DingTalk.AccessTokenContainer.TryGetToken(_view_account.corpId);
            var result = Li.DingTalk.Model.Contacts.media.Getuserenterprisespace(access_token, agent_id, domain, type, userid, path, fileids, duration);
            if (result.IsOK())
            {
                return Content("<script>window.parent.alert('授权用户访问企业下的自定义空间成功!')</script>");
            }
            else
            {
                return Content("<script>window.parent.alert('授权用户访问企业下的自定义空间失败!')</script>");
            }
        }

        /// <summary>
        /// 开启文件上传事务
        /// </summary>
        /// <param name="agent_id">微应用的agentId</param>
        /// <param name="file_size">文件大小</param>
        /// <param name="chunk_numbers">文件总块数</param>
        /// <param></param>
        /// <returns></returns>
        public ActionResult OpenFileUpload(string agent_id, int file_size, int chunk_numbers)
        {
            var access_token = Li.DingTalk.AccessTokenContainer.TryGetToken(_view_account.corpId);
            var result = Li.DingTalk.Model.Contacts.media.Openfileupload(access_token, agent_id, file_size, chunk_numbers);
            if (result.IsOK())
            {
                return Content("<script>window.parent.alert('开启文件上传事务成功!')</script>");
            }
            else
            {
                return Content("<script>window.parent.alert('开启文件上传事务失败!')</script>");
            }
        }

        /// <summary>
        /// 上传文件块
        /// </summary>
        /// <param name="agent_id">微应用的agentId</param>
        /// <param name="upload_id">上传事务id 需要utf-8 urlEncode</param>
        /// <param name="chunk_sequence">文件块号，从1开始计数</param>
        /// <param></param>
        /// <returns></returns>
        public ActionResult UploadFileFast(string agent_id, string upload_id, int chunk_sequence)
        {
            var access_token = Li.DingTalk.AccessTokenContainer.TryGetToken(_view_account.corpId);
            var result = Li.DingTalk.Model.Contacts.media.Uploadfilefast(access_token, agent_id, upload_id, chunk_sequence);
            if (result.IsOK())
            {
                return Content("<script>window.parent.alert('上传文件块成功!')</script>");
            }
            else
            {
                return Content("<script>window.parent.alert('上传文件块失败!')</script>");
            }
        }

        /// <summary>
        /// 提交文件上传事务
        /// </summary>
        /// <param name="agent_id">微应用的agentId</param>
        /// <param name="file_size">文件大小</param>
        /// <param name="chunk_numbers">文件总块数</param>
        /// <param name="upload_id">上传事务id 需要utf-8 urlEncode</param>
        /// <param></param>
        /// <returns></returns>
        public ActionResult ExtractingFileUpload(string agent_id, int file_size, int chunk_numbers, string upload_id)
        {
            var access_token = Li.DingTalk.AccessTokenContainer.TryGetToken(_view_account.corpId);
            var result = Li.DingTalk.Model.Contacts.media.Extractingfileupload(access_token, agent_id, file_size, chunk_numbers, upload_id);
            if (result.IsOK())
            {
                return Content("<script>window.parent.alert('提交文件上传事务成功!')</script>");
            }
            else
            {
                return Content("<script>window.parent.alert('提交文件上传事务失败!')</script>");
            }
        }

        /// <summary>
        /// 单步文件上传
        /// </summary>
        /// <param name="agent_id">微应用的agentId</param>
        /// <param name="file_size">文件大小</param>
        /// <param></param>
        /// <returns></returns>
        public ActionResult SingleFileUpload(string agent_id, int file_size)
        {
            var access_token = Li.DingTalk.AccessTokenContainer.TryGetToken(_view_account.corpId);
            var result = Li.DingTalk.Model.Contacts.media.Singlefileupload(access_token, agent_id, file_size);
            if (result.IsOK())
            {
                return Content("<script>window.parent.alert('单步文件上传成功!')</script>");
            }
            else
            {
                return Content("<script>window.parent.alert('单步文件上传失败!')</script>");
            }
        }

        #endregion

        #endregion

        #region 微应用管理

        /// <summary>
        /// 创建微应用
        /// </summary>
        /// <param name="cms">创建微应用的结构体</param>
        /// <param></param>
        /// <returns></returns>
        public ActionResult CreateMicroapplication(createmicroapplicationstructural cms)
        {
            var access_token = Li.DingTalk.AccessTokenContainer.TryGetToken(_view_account.corpId);
            var result = Li.DingTalk.Model.Contacts.microappHD.Createmicroapplication(access_token, cms);
            if (result.IsOK())
            {
                return PartialView(result);
            }
            else
            {
                return Content("<script>window.parent.alert('创建微应用失败!')</script>");
            }
        }

        /// <summary>
        /// 更新微应用
        /// </summary>
        /// <param name="ums">更新微应用的结构体</param>
        /// <param></param>
        /// <returns></returns>
        public ActionResult UpdateMicroapplication(updatemicroapplicationstructural ums)
        {
            var access_token = Li.DingTalk.AccessTokenContainer.TryGetToken(_view_account.corpId);
            var result = Li.DingTalk.Model.Contacts.microappHD.Updatemicroapplication(access_token, ums);
            if (result.IsOK())
            {
                return PartialView(result);
            }
            else
            {
                return Content("<script>window.parent.alert('更新微应用失败!')</script>");
            }
        }

        /// <summary>
        /// 删除微应用
        /// </summary>
        /// <param name="dms">删除微应用的结构体</param>
        /// <param></param>
        /// <returns></returns>
        public ActionResult DeleteMicroapplication(deletemicroapplicationstructural dms)
        {
            var access_token = Li.DingTalk.AccessTokenContainer.TryGetToken(_view_account.corpId);
            var result = Li.DingTalk.Model.Contacts.microappHD.Deletemicroapplication(access_token, dms);
            if (result.IsOK())
            {
                return Content("<script>window.parent.alert('删除微应用成功!')</script>");
            }
            else
            {
                return Content("<script>window.parent.alert('删除微应用失败!')</script>");
            }
        }

        /// <summary>
        /// 列出微应用
        /// </summary>
        /// <param></param>
        /// <returns></returns>
        public ActionResult MicroapplicationList(microapplicationliststructural mls)
        {
            var access_token = Li.DingTalk.AccessTokenContainer.TryGetToken(_view_account.corpId);
            var result = Li.DingTalk.Model.Contacts.microappHD.Microapplicationlist(access_token, mls);
            if (result.IsOK())
            {
                return PartialView(result);
            }
            else
            {
                return Content("<script>window.parent.alert('列出微应用失败!')</script>");
            }
        }

        /// <summary>
        /// 获取企业设置的微应用可见范围
        /// </summary>
        /// <param name="oms">获取企业设置的微应用可见范围的结构体</param>
        /// <param></param>
        /// <returns></returns>
        public ActionResult ObtainMicroapplicationVisible(obtainmicroapplicationvisiblestructural oms)
        {
            var access_token = Li.DingTalk.AccessTokenContainer.TryGetToken(_view_account.corpId);
            var result = Li.DingTalk.Model.Contacts.microappHD.Obtainmicroapplicationvisible(access_token, oms);
            if (result.IsOK())
            {
                return PartialView(result);
            }
            else
            {
                return Content("<script>window.parent.alert('获取企业设置的微应用可见范围失败!')</script>");
            }
        }

        /// <summary>
        /// 设置微应用的可见范围
        /// </summary>
        /// <param name="sms">设置微应用的可见范围的结构体</param>
        /// <param></param>
        /// <returns></returns>
        public ActionResult SetMicroapplicationVisible(setmicroapplicationvisiblestructural sms)
        {
            var access_token = Li.DingTalk.AccessTokenContainer.TryGetToken(_view_account.corpId);
            var result = Li.DingTalk.Model.Contacts.microappHD.Setmicroapplicationvisible(access_token, sms);
            if (result.IsOK())
            {
                return PartialView(result);
            }
            else
            {
                return Content("<script>window.parent.alert('设置微应用的可见范围失败!')</script>");
            }
        }

        /// <summary>
        /// 获取指定微应用下指定用户绑定的全部规则
        /// </summary>
        /// <param name="gar">获取指定微应用下指定用户绑定的全部规则的结构体</param>
        /// <param></param>
        /// <returns></returns>
        public ActionResult GetAllRules(getallrulesstructural gar)
        {
            var access_token = Li.DingTalk.AccessTokenContainer.TryGetToken(_view_account.corpId);
            var result = Li.DingTalk.Model.Contacts.microappHD.Getallrules(access_token, gar);
            if (result.IsOK())
            {
                return PartialView(result);
            }
            else
            {
                return Content("<script>window.parent.alert('获取指定微应用下指定用户绑定的全部规则失败!')</script>");
            }
        }

        /// <summary>
        /// 获取规则绑定的用户数
        /// </summary>
        /// <param name="gcs">获取规则绑定的用户数的结构体</param>
        /// <param></param>
        /// <returns></returns>
        public ActionResult GetRulesUserCount(getrulesusercountstructural gcs)
        {
            var access_token = Li.DingTalk.AccessTokenContainer.TryGetToken(_view_account.corpId);
            var result = Li.DingTalk.Model.Contacts.microappHD.Getrulesusercount(access_token, gcs);
            if (result.IsOK())
            {
                return PartialView(result);
            }
            else
            {
                return Content("<script>window.parent.alert('获取规则绑定的用户数失败!')</script>");
            }
        }

        /// <summary>
        /// 删除规则
        /// </summary>
        /// <param name="drs">删除规则的结构体</param>
        /// <param></param>
        /// <returns></returns>
        public ActionResult DeleteRules(deleterulesstructural drs)
        {
            var access_token = Li.DingTalk.AccessTokenContainer.TryGetToken(_view_account.corpId);
            var result = Li.DingTalk.Model.Contacts.microappHD.Deleterules(access_token, drs);
            if (result.IsOK())
            {
                return Content("<script>window.parent.alert('删除规则成功!')</script>");
            }
            else
            {
                return Content("<script>window.parent.alert('删除规则失败!')</script>");
            }
        }

        #endregion

        #region 钉钉运动
        /// <summary>
        /// 获取人员信息
        /// </summary>
        /// <returns></returns>
        public ActionResult stepinfo()
        {
            var CorpID = ConfigHelper.FetchCorpID();
          
                //检查是否注册
            if (!Li.DingTalk.AccessTokenContainer.CheckRegistered(CorpID))
            {
                //查询CorpSecret
                var CorpSecret = ConfigHelper.FetchCorpSecret();
                Li.DingTalk.AccessTokenContainer.Register(CorpID, CorpSecret);
            }
            var access_token = Li.DingTalk.AccessTokenContainer.TryGetToken(CorpID);

            IDingTalkClient client = new DingTalk.Api.DefaultDingTalkClient("https://eco.taobao.com/router/rest");
            CorpHealthStepinfoListbyuseridRequest req = new CorpHealthStepinfoListbyuseridRequest();
            req.Userids = "0957482150676115,0711466326115392";
            req.StatDate = "20180517";
            CorpHealthStepinfoListbyuseridResponse rsp = client.Execute(req, access_token);
            Console.WriteLine(rsp.Body);

            return Content("");
           
            //if (result.IsOK())
            //{
            //    return PartialView(result);
            //}
            //else
            //{
            //    return Content("获取成员信息失败");
            //}
        }
        #endregion

    }
}